package com.dkm.nettysocketdemo.myWebsite.service.impl;

import com.dkm.nettysocketdemo.myWebsite.dao.HelmetGroupMapper;
import com.dkm.nettysocketdemo.myWebsite.dao.HelmetModMapper;
import com.dkm.nettysocketdemo.myWebsite.domain.HelmetClient;
import com.dkm.nettysocketdemo.myWebsite.domain.HelmetGroup;
import com.dkm.nettysocketdemo.myWebsite.service.HelmetGroupService;
import com.dkm.nettysocketdemo.untils.redis.RedisOperator;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;


/**
 *  @ClassName : HelmetGroupServiceImpl  //类名
 *  @Description : 安全帽分组service层实现类 //描述
 *  @Author : zmh //作者
 *  @Date: 2021-3-17 09:28  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HelmetGroupServiceImpl implements HelmetGroupService {

    @Resource
    HelmetGroupMapper helmetGroupMapper;

    @Resource
    HelmetModMapper helmetModMapper;

    @Resource
    RedisOperator redisOperator;

    Gson gson = new Gson();

    /**
     * 删除单条安全帽组
     *
     * @param pkGroupId 分组id
     * @return 回调函数
     */
    @Override
    public int deleteById(String pkGroupId) {
        //查询需要删除的分组
        HelmetGroup group = gson.fromJson((String) redisOperator.get(pkGroupId), HelmetGroup.class);
        if (ObjectUtils.isEmpty(group)) {
            group = helmetGroupMapper.selectByPrimaryKey(pkGroupId);
        }
        //将改分组下的所有安全帽分组设置为空
        helmetModMapper.updateGroupId(pkGroupId);
        //删除改分组
        int row = helmetGroupMapper.deleteByPrimaryKey(pkGroupId);
        if (row > 0) {
            redisOperator.del(pkGroupId);
            redisOperator.lRemove(group.getClientId() + "ClientList", 0, gson.toJson(group));
        }
        return row;
    }

    /**
     * 增加安全帽分组
     *
     * @param helmetGroup 分组对象
     * @param client      当前登录人
     * @return 回调函数
     */
    @Override
    public int insertHelmetGroup(HelmetGroup helmetGroup, HelmetClient client) {
        helmetGroup.setPkGroupId(UUID.randomUUID().toString().replaceAll("-", ""));
        helmetGroup.setIsRevision("1");
        helmetGroup.setCreatedBy(client.getClientName());
        int row = helmetGroupMapper.insertSelective(helmetGroup);
        if (row > 0) {
            redisOperator.set(helmetGroup.getPkGroupId(), gson.toJson(helmetGroup));
            redisOperator.lSet(helmetGroup.getClientId() + "ClientList", gson.toJson(helmetGroup));
        }
        return row;
    }

    /**
     * 修改安全帽分组
     *
     * @param helmetGroup 分组模型
     * @param client      当前登录人
     * @return 回调函数
     */
    @Override
    public int updateHelmetGroup(HelmetGroup helmetGroup, HelmetClient client) {
        HelmetGroup group = gson.fromJson((String) redisOperator.get(helmetGroup.getPkGroupId()), HelmetGroup.class);
        if (ObjectUtils.isEmpty(group)) {
            group = helmetGroupMapper.selectByPrimaryKey(helmetGroup.getPkGroupId());
        }
        helmetGroup.setUpdatedTime(new Date());
        helmetGroup.setUpdatedBy(client.getClientName());
        int row = helmetGroupMapper.updateByPrimaryKeySelective(helmetGroup);
        if (row > 0) {
            redisOperator.lRemove(group.getClientId() + "ClientList", 0, gson.toJson(group));
            redisOperator.set(helmetGroup.getPkGroupId(), helmetGroup);
        }
        return row;
    }

    /**
     * 根据id查询安全帽组
     *
     * @param pkGroupId 分组id
     * @return 分组对象
     */
    @Override
    public HelmetGroup findById(String pkGroupId) {
        HelmetGroup group = gson.fromJson((String) redisOperator.get(pkGroupId), HelmetGroup.class);
        if (ObjectUtils.isEmpty(group)) {
            group = helmetGroupMapper.selectByPrimaryKey(pkGroupId);
            redisOperator.set(pkGroupId, gson.toJson(group));
        }
        return group;
    }

    /**
     * 分页查询所有安全帽
     *
     * @param helmetGroup 分组名称
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param page        当前页数
     * @param limit       显示行数
     * @return 查询集合
     */
    @Override
    public List<HelmetGroup> selectAll(HelmetGroup helmetGroup, String startTime, String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return helmetGroupMapper.selectAll(helmetGroup, startTime, endTime);
    }

    /**
     * 查询总安全帽分组数
     *
     * @param helmetGroup 分组名称
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return 总行数
     */
    @Override
    public Integer rowCount(HelmetGroup helmetGroup, String startTime, String endTime) {
        return helmetGroupMapper.rowCount(helmetGroup, startTime, endTime);
    }

    /**
     * 批量删除安全帽分组
     *
     * @param list id集合
     * @return 回调函数
     */
    @Override
    public int deleteAll(List<String> list) {
        int row = helmetGroupMapper.deleteAll(list);
        helmetModMapper.updateGroupAll(list);
        if (row > 0) {
            for (String s : list) {
                HelmetGroup group = gson.fromJson((String) redisOperator.get(s), HelmetGroup.class);
                if (!ObjectUtils.isEmpty(group)) {
                    redisOperator.lRemove(group.getClientId() + "ClientList", 0, redisOperator.get(s));
                }
            }
            redisOperator.del(list);
        }
        return row;
    }

    /**
     * 根据客户id查询分组
     *
     * @param clientId 客户id
     * @return 分组集合
     */
    @Override
    public List<HelmetGroup> findClientIdAll(String clientId) {
        List<String> list = redisOperator.lGet(clientId + "ClientList", 0, -1);
        List<HelmetGroup> groups;
        if (ObjectUtils.isEmpty(list)) {
            groups = helmetGroupMapper.findClientId(clientId);
            for (HelmetGroup group : groups) {
                redisOperator.lSet(clientId + "ClientList", gson.toJson(group));
            }
        } else {
            groups = new LinkedList<>();
            for (String s : list) {
                HelmetGroup group = gson.fromJson(s, HelmetGroup.class);
                groups.add(group);
            }
        }
        return groups;
    }

    /**
     * 修改分组名称
     *
     * @param groupName 分组名称
     * @param groupId   分组id
     * @return 回调函数
     */
    @Override
    public int updateGroupName(String groupName, String groupId) {
        int row = helmetGroupMapper.updateGroupName(groupName, groupId);
        if (row > 0) {
            HelmetGroup group = gson.fromJson((String) redisOperator.get(groupId), HelmetGroup.class);
            if (!ObjectUtils.isEmpty(group)) {
                redisOperator.lRemove(group.getClientId() + "ClientList", 0, gson.toJson(group));
            }
            group = helmetGroupMapper.selectByPrimaryKey(groupId);
            redisOperator.lSet(group.getClientId() + "ClientList", gson.toJson(group));
            redisOperator.set(groupId, group);
        }
        return row;
    }

}
